package com.example.springboottest.student.service.impl;

import com.example.springboottest.base.AbstractService;
import com.example.springboottest.constant.Constant;
import com.example.springboottest.student.event.StuEvent;
import com.example.springboottest.student.model.Student;
import com.example.springboottest.student.service.StuJdbcService;
import com.example.springboottest.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service("stuJdbcService")
public class StuJdbcServiceImpl extends AbstractService implements StuJdbcService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 上下文对象
     */
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public List<Student> doFindALl() {
        String sql = "select * from test.student";
        List<Student> userList = (List<Student>) jdbcTemplate.query(sql, new RowMapper<Student>() {
                    @Override
                    public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
                        Student user = new Student(rs.getString("id"), rs.getString("name"), rs.getInt("age"), rs.getString("address"));
                        return user;
                    }
                }
        );
        logger.info("doFindById");
        userList.stream().forEach(stu -> logger.info(stu.toString()));
//        stuNoService.showMessage();
        return userList;
    }

    @Override
    public void doDeleteById(String id) {
        applicationContext.publishEvent(new StuEvent(this, Constant.EVENT_TYPE_DELETE, id));
        jdbcTemplate.update("delete from student where id = ?", id);
    }

    @Override
    public void doCreate(Student student) {
        String id = IdUtil.getUUID();
        jdbcTemplate.update("insert into student values(?,?,?,?)", id, student.getName(), student.getAge(), student.getAddress());
        applicationContext.publishEvent(new StuEvent(this, Constant.EVENT_TYPE_CREATE, id));
    }

    @Override
    public void doUpdate(Student student) {
        jdbcTemplate.update("update student set \"name\" =?,age=?,address =? where id = ?",
                student.getName(), student.getAge(), student.getAddress(), student.getId());
    }

    @Override
    public void idoUpdate(Student student) {

    }

    @Override
    public Student doFindById(String id) {
        String sql = "select * from student where id =?";
        Object[] params = new Object[]{id};
        return jdbcTemplate.queryForObject(sql, params, new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
                Student student = new Student(rs.getString("id"), rs.getString("name"), rs.getInt("age"), rs.getString("address"));
                logger.info("{}doFindById{}", 123, "--oo");
                logger.error(student.toString());
                return student;
            }
        });
    }

    @Override
    public void doUpdateLock(Student student) {

    }

    @Override
    public Page<Student> doPage() {
        return null;
    }

    @Override
    public String doLock() {
        return "stop";
    }
}